/// <reference path="../../wx/ad/WxVideoAd.ts" />
namespace mgp.ads {
    export class MzVideoAd extends WxVideoAd {

        protected _videoAd: any;

        /** 魅族的视频激励广告是否能领奖了 */
        protected _videoCanReward = false;

        protected _getVideoAd(id?: string) {
            if (!this._videoAd) {
                this._videoAd = qg.createRewardedVideoAd({
                    adUnitId: id || this._info.ids[0]
                });
                this._addListener();
            }
            return this._videoAd;
        }

        protected _createdAd() {
            if (!this._videoAd) return;
            this.loadVideoAd();
        }

        protected _addListener() {
            if (!this._videoAd) return;
            super._addListener();
            this._videoAd.onRewarded(this._onVideoRewarded.bind(this))
        }
        protected _removeListener() {
            if (!this._videoAd) return;
            super._addListener();
            this._videoAd.offRewarded(this._onVideoRewarded.bind(this))
        }
        protected _onVideoRewarded() {
            debugLog('激励视频广告完成，准备发放奖励')
            this._videoCanReward = true;
        }

        protected _videoPlay() {
            this._videoAd.show();
            /* this._videoAd.show().then(() => {

            }).catch(err => {
                this.loadVideoAd()
            }) */

            debugLog('激励视频开始播放')
            if (!!this._info.startPlay) this._info.startPlay();
            this._readyPlayVideo = false;
        }

        protected _onVideoClose(res) {
            // 用户点击了【关闭广告】按钮
            if (this._videoCanReward) {
                // 正常播放结束，可以下发游戏奖励
                debugLog('正常播放结束，可以下发游戏奖励')
                if (!!this._successFun) this._successFun()
                if (!!this._info.getReward) this._info.getReward();
            } else {
                // 播放中途退出，不下发游戏奖励
                debugLog('播放中途退出，不下发游戏奖励')
                if (!!this._cancelFun) this._cancelFun()
                if (!!this._info.videoCancel) this._info.videoCancel();
            }

            debugLog('视频关闭')
            if (!!this._completeFun) this._completeFun()
            if (!!this._info.videoComplete) this._info.videoComplete();

        }
    }
}